package com.yyx.app1.Question_function.UI;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import com.yyx.app1.Data.Question.Chat_Data;
import com.yyx.app1.Data.Question.Question_Activity_Data;
import com.yyx.app1.MyApp;
import com.yyx.app1.Question_function.Control.ChatListView.ListView_Chat_Adapter;
import com.yyx.app1.R;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * Created by yuexing on 2015/9/24.
 */
@EActivity(R.layout.activity_chat)
public class Chat_Activity extends Activity {
    private ListView_Chat_Adapter adapter;
    private Question_Activity_Data head;
    @ViewById
    ListView chat_listview;

    @ViewById
    EditText send_edittext;

    @AfterViews
    public void inti()
    {
        head = (Question_Activity_Data)getIntent().getExtras().get("Data");
        adapter=new ListView_Chat_Adapter(this);
        adapter.findAll(head);
        adapter.findAll();
        chat_listview.setAdapter(adapter);
        chat_listview.setOverScrollMode(View.OVER_SCROLL_NEVER);
    }

    @Click
    public void send_button()
    {
        String str = send_edittext.getText().toString();
        if (str.isEmpty())
            return;
        send_edittext.setText("");
        adapter.add(new Chat_Data(((MyApp) getApplication()).getBossInfo().getId(), head.getUsername(), Chat_Data.Chat_Type.TEXT, str, null, null));
        chat_listview.setSelection(1);
    }

    @Click
    public void activity_chat_addpicture()
    {
        Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        try
        {//系统应用获取图片
            Uri uri = data.getData();
            Log.i("正常", "uri=" + uri.toString() + "requestCode=" + requestCode);
            adapter.add(new Chat_Data(((MyApp) getApplication()).getBossInfo().getId(),head.getUsername(), Chat_Data.Chat_Type.IMAGE,null,null,uri));
            chat_listview.setSelection(1);
            super.onActivityResult(requestCode, resultCode, data);
        }
        catch (Exception e)
        {
            Log.e("异常1510211730",e.toString());
        }
    }

    @Click
    public void home()
    {
        finish();
    }

    @Click
    public void activity_chat_pet_info()
    {
        startActivity(new Intent(Chat_Activity.this, PetInfo_Activity_.class));
    }

    @Override
    protected void onDestroy() {
        int position = getIntent().getIntExtra("position",20);
        Intent intent = new Intent();
        intent.putExtra("position",position);
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            home();
        }
        return true;
    }
}
